﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.BLL;
using WHC.Hotel.Entity;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmRoom : BaseForm
    {
        public FrmRoom()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CListItem item = this.cmbRoomType.SelectedItem as CListItem;
            if (item != null)
            {
                SearchCondition condition = new SearchCondition();
                condition.AddCondition("RoomType", item.Value, SqlOperator.Equal, true);

                string sql = condition.BuildConditionSql(DatabaseType.SqlServer).Replace("Where", "");
                this.dgRoom.DataSource = BLLFactory<Room>.Instance.FindToDataTable(sql);
            }
        }

        private void BindData()
        {
            this.dgRoom.AutoGenerateColumns = false;
            this.dgType.AutoGenerateColumns = false;

            List<RoomTypeInfo> list = BLLFactory<RoomType>.Instance.GetAll();
            this.cmbRoomType.Items.Clear();
            foreach (RoomTypeInfo typeInfo in list)
            {
                CListItem item = new CListItem(typeInfo.Name, typeInfo.Name);
                this.cmbRoomType.Items.Add(item);
            }
            this.cmbRoomType.Items.Insert(0, new CListItem("所有房间", ""));
            this.cmbRoomType.SelectedIndex = 0;

            #region 列表绑定
            this.dgType.DataSource = BLLFactory<RoomType>.Instance.GetAllToDataTable();
            this.dgType.Columns[0].DataPropertyName = "ID";
            this.dgType.Columns[1].DataPropertyName = "Name";
            this.dgType.Columns[2].DataPropertyName = "BedsNumber";
            this.dgType.Columns[3].DataPropertyName = "DayPrice";
            this.dgType.Columns[4].DataPropertyName = "HalfPrice";
            this.dgType.Columns[5].DataPropertyName = "DepositMoney";
            this.dgType.Columns[6].DataPropertyName = "HourPrice";
            this.dgType.Columns[7].DataPropertyName = "EnablePartTime";
            this.dgType.Columns[8].DataPropertyName = "Note";

            this.dgRoom.DataSource = BLLFactory<Room>.Instance.GetAllToDataTable();
            this.dgRoom.Columns[0].DataPropertyName = "ID";
            this.dgRoom.Columns[1].DataPropertyName = "RoomNo";
            this.dgRoom.Columns[2].DataPropertyName = "RoomType";
            this.dgRoom.Columns[3].DataPropertyName = "Status";
            this.dgRoom.Columns[4].DataPropertyName = "BuildNum";
            this.dgRoom.Columns[5].DataPropertyName = "Floor"; 
            this.dgRoom.Columns[6].DataPropertyName = "RoomTelephone";
            this.dgRoom.Columns[7].DataPropertyName = "LockNumber"; 
            #endregion
        }

        private void FrmRoom_Load(object sender, EventArgs e)
        {
            BindData();
        }

        #region 按钮操作
        private void btnAddType_Click(object sender, EventArgs e)
        {
            FrmEditRoomType dlg = new FrmEditRoomType();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                BindData();
            }
        }

        private void btnEditType_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in this.dgType.SelectedRows)
            {
                FrmEditRoomType dlg = new FrmEditRoomType();
                dlg.ID = row.Cells[0].Value.ToString();
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    BindData();
                }

                break;
            }
        }

        private void btnDeleteType_Click(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            foreach (DataGridViewRow row in this.dgType.SelectedRows)
            {
                BLLFactory<RoomType>.Instance.Delete(row.Cells[0].Value.ToString());
                BindData();
            }
        }

        private void btnDiscount_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in this.dgType.SelectedRows)
            {
                FrmRoomDiscount dlg = new FrmRoomDiscount();
                dlg.ID = row.Cells[0].Value.ToString();
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    BindData();
                }

                break;
            }
        }

        private void btnAddOne_Click(object sender, EventArgs e)
        {
            FrmEditRoom dlg = new FrmEditRoom();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                BindData();
            }
        }

        private void btnAddMulti_Click(object sender, EventArgs e)
        {
            FrmEditRoomMulti dlg = new FrmEditRoomMulti();
            if (DialogResult.OK == dlg.ShowDialog())
            {
                BindData();
            }
        }

        private void btnEditRoom_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in this.dgRoom.SelectedRows)
            {
                FrmEditRoom dlg = new FrmEditRoom();
                dlg.ID = row.Cells[0].Value.ToString();
                if (DialogResult.OK == dlg.ShowDialog())
                {
                    BindData();
                }

                break;
            }
        }

        private void btnDeleteRoom_Click(object sender, EventArgs e)
        {
            if (MessageUtil.ShowYesNoAndTips("您确定删除选定的记录么？") == DialogResult.No)
            {
                return;
            }

            foreach (DataGridViewRow row in this.dgRoom.SelectedRows)
            {
                BLLFactory<Room>.Instance.Delete(row.Cells[0].Value.ToString());
                BindData();
            }
        } 
        #endregion

        #region 菜单操作
        private void menu_Type_AddType_Click(object sender, EventArgs e)
        {
            btnAddType_Click(null, null);
        }

        private void menu_Type_Edit_Click(object sender, EventArgs e)
        {
            btnEditType_Click(null, null);
        }

        private void menu_Type_Delete_Click(object sender, EventArgs e)
        {
            btnDeleteType_Click(null, null);
        }

        private void menu_Type_Discount_Click(object sender, EventArgs e)
        {
            btnDiscount_Click(null, null);
        }

        private void menu_Room_AddOne_Click(object sender, EventArgs e)
        {
            btnAddOne_Click(null, null);
        }

        private void menu_Room_AddMulti_Click(object sender, EventArgs e)
        {
            btnAddMulti_Click(null, null);
        }

        private void menu_Room_Edit_Click(object sender, EventArgs e)
        {
            btnEditRoom_Click(null, null);
        }

        private void menu_Room_Delete_Click(object sender, EventArgs e)
        {
            btnDeleteRoom_Click(null, null);
        }

        private void menu_Type_Update_Click(object sender, EventArgs e)
        {
            BindData();
        }

        private void menu_Room_Update_Click(object sender, EventArgs e)
        {
            BindData();
        }

        #endregion

        #region DataGridView双击操作
        private void dgType_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEditType_Click(null, null);
        }

        private void dgRoom_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnEditRoom_Click(null, null);
        } 
        #endregion

        private void btnRoomGift_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in this.dgType.SelectedRows)
            {
                string ID = row.Cells[0].Value.ToString();
                if (!string.IsNullOrEmpty(ID))
                {
                    RoomTypeInfo typeInfo = BLLFactory<RoomType>.Instance.FindByID(ID);
                    FrmRoomInitConsumer dlg = new FrmRoomInitConsumer();
                    dlg.RoomType = typeInfo;
                    dlg.ShowDialog();

                    break;
                }
            }
        }

        private void menu_Type_InitRoomConsumer_Click(object sender, EventArgs e)
        {
            btnRoomGift_Click(null, null);
        }

    }
}
